You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
CiStrings.indexOf (the case-insensitive Strings.CI.indexOf) bails out only when startPos > endLimit, where endLimit = str.length() - searchStr.length() + 1.
For an empty search endLimit is str.length() + 1, so a startPos of exactly str.length() + 1 slips past the guard and the empty-search branch returns startPos unchanged.
Repro: Strings.CI.indexOf("abc", "", 4)
Expected: -1 (the class Javadoc documents out-of-range start positions as -1, e.g. Strings.CI.indexOf("abc", "", 9) = -1)
Actual: 4, an index one past the end of a length-3 string
The case-sensitive CS.indexOf delegates to String.indexOf and clamps, so only the hand-rolled CI implementation is affected. Changed the guard to startPos >= endLimit; -1 is returned once startPos passes the last valid empty-match position (str.length()), and every documented example is unchanged.
I used AI to create any part of, or all of, this pull request. Which AI tool was used to create this pull request, and to what extent did it contribute?
Run a successful build using the default Maven goal with mvn; that's mvn on the command line by itself.
Write unit tests that match behavioral changes, where the tests fail if the changes to the runtime are not applied. This may not always be possible, but it is a best practice.
Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
Each commit in the pull request should have a meaningful subject line and body.
garydgregory
changed the title
fix Strings.CI.indexOf returning out-of-range index for empty search
Fix Strings.CI.indexOf returning out-of-range index for empty search
Jun 30, 2026
garydgregory
changed the title
Fix Strings.CI.indexOf returning out-of-range index for empty search
Fix Strings.CI.indexOf() returning out-of-range index for empty search
Jun 30, 2026
Had a look through the rest of the class. The empty-search paths in lastIndexOf, contains, startsWith/endsWith, equals and the *Any/remove/replace helpers all behave the same for CI and CS.
The one spot left is indexOf with a start position past the end:
CS delegates to String.indexOf, which clamps the start to the length and so keeps matching the empty search; the hand-rolled CI returns -1 once startPos > length. The shared prose says 'a start position greater than the string length only matches an empty search CharSequence', which lines up with the CS side. Want me to make CI clamp to match CS (and drop the CI.indexOf("abc", "", 9) = -1 example), or keep the documented -1?
Had a look through the rest of the class. The empty-search paths in lastIndexOf, contains, startsWith/endsWith, equals and the *Any/remove/replace helpers all behave the same for CI and CS.
The one spot left is indexOf with a start position past the end:
CS delegates to String.indexOf, which clamps the start to the length and so keeps matching the empty search; the hand-rolled CI returns -1 once startPos > length. The shared prose says 'a start position greater than the string length only matches an empty search CharSequence', which lines up with the CS side. Want me to make CI clamp to match CS (and drop the CI.indexOf("abc", "", 9) = -1 example), or keep the documented -1?
@alhudz
Yeah, the reply should be consistent, regardless of case.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
CiStrings.indexOf(the case-insensitiveStrings.CI.indexOf) bails out only whenstartPos > endLimit, whereendLimit = str.length() - searchStr.length() + 1.endLimitisstr.length() + 1, so astartPosof exactlystr.length() + 1slips past the guard and the empty-search branch returnsstartPosunchanged.Repro:
Strings.CI.indexOf("abc", "", 4)Expected:
-1(the class Javadoc documents out-of-range start positions as-1, e.g.Strings.CI.indexOf("abc", "", 9) = -1)Actual:
4, an index one past the end of a length-3stringThe case-sensitive
CS.indexOfdelegates toString.indexOfand clamps, so only the hand-rolledCIimplementation is affected. Changed the guard tostartPos >= endLimit;-1is returned oncestartPospasses the last valid empty-match position (str.length()), and every documented example is unchanged.mvn; that'smvnon the command line by itself.